package com.ysstech.kettle.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.JsonUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.kettle.entity.JobParam;
import com.ysstech.kettle.service.JobParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * kettle参数信息表 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-10-17
 */
@Controller
@Slf4j
@Api(tags = "参数管理api")
@RequestMapping("/jobParam")
public class JobParamController extends BaseController {

    @Autowired
    private JobParamService jobParamService;

    @YssPermissions(value = {"kettle:job:param"})
    @ApiOperation(value = "跳转参数管理管理页面")
    @YssLog(module = "参数管理管理", remark = "跳转参数管理管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "kettle/jobParam";
    }

    @YssPermissions(value = {"kettle:job:param"})
    @ApiOperation(value = "获取参数管理页面初始值")
    @YssLog(module = "参数管理管理", remark = "获取参数管理管理页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }

    /**
     * 获取参数管理信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:param"})
    @ApiOperation(value = "获取参数管理信息")
    @YssLog(module = "参数管理管理", remark = "获取参数管理信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(JobParam jobParam, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取参数管理信息成功",
                jobParamService.queryAllJobParam(jobParam, this.getPage(page, rows)));
    }

    /**
     * 获取参数管理信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:param"})
    @ApiOperation(value = "获取参数管理信息")
    @YssLog(module = "参数管理管理", remark = "获取参数管理信息")
    @PostMapping(value = "/listAllInfo")
    @ResponseBody
    public RestResult listAllInfo() throws Exception {
        QueryWrapper<JobParam> wrapper = new QueryWrapper<>();
        wrapper.eq("STATUS", 1);
        wrapper.orderByAsc("SEQUENCE");
        return ResultGenerator.getSuccessResult("获取参数管理信息成功",
                jobParamService.list(wrapper));
    }

    /**
     * 新增参数管理的方法
     *
     * @param jobParam
     * @return
     */
    @YssPermissions(value = {"kettle:job:param:add"})
    @ApiOperation(value = "新增参数管理信息")
    @YssLog(module = "参数管理管理", remark = "新增参数管理信息")
    @RequestMapping(value = "/addJobParam")
    @ResponseBody
    public RestResult addJobParam(JobParam jobParam) throws Exception {
        jobParam.setId(DateUtil.generateUUID());
        jobParam.setAddTime(LocalDateTime.now());
        jobParamService.save(jobParam);
        return ResultGenerator.getSuccessResult("新增参数管理信息成功！");
    }

    /**
     * 修改参数管理信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:param:edit"})
    @ApiOperation(value = "修改参数管理信息")
    @YssLog(module = "参数管理管理", remark = "修改参数管理信息")
    @RequestMapping(value = "/editJobParam")
    @ResponseBody
    public RestResult editJobParam(JobParam jobParam) throws Exception {
        if (jobParam == null || StringUtils.isBlank(jobParam.getId())) {
            throw new BusinessException("获取参数管理信息失败，请稍后重试！");
        }
        jobParam.setEditTime(LocalDateTime.now());
        jobParamService.updateById(jobParam);
        return ResultGenerator.getSuccessResult("修改参数管理信息成功！");
    }

    /**
     * 删除参数管理信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:param:del"})
    @ApiOperation(value = "删除参数管理信息")
    @YssLog(module = "参数管理管理", remark = "删除参数管理信息")
    @RequestMapping(value = "/delJobParam")
    @ResponseBody
    public RestResult delJobParam(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取删除参数管理信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        jobParamService.removeByIds(list);
        return ResultGenerator.getSuccessResult("删除参数管理信息成功！");
    }

}
